package lolnotepad;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author havlicekdom@gmail.com
 */
public class Writer implements ActionListener {

    public void output(String outputLine) {      //function for writing data into the file
        try (BufferedWriter output = new BufferedWriter(new FileWriter("./src/data.src", true));) {
            outputLine = outputLine.replaceAll("\\\n", " ");
            output.write(outputLine);
            output.newLine();
            output.close();
            JOptionPane.showMessageDialog(null, "Save succesful!");
        } catch (IOException ioee) {
            try {
                File soubor = new File("./src/data.src");
                soubor.createNewFile();
            } catch (IOException ioe) {
                System.err.println("File creation failed.");
            }
        }
    }

    public Writer(final JFrame frame, ArrayList<String> champions, ArrayList<String> roles, ArrayList<String> leagues) {
        
            frame.setTitle("LoL Notepad - writing new note");
            JLabel label1 = new JLabel("Champion: ");
            final JComboBox comboBox1 = new JComboBox();
            JPanel panel1 = new JPanel();
            panel1.setLayout(new FlowLayout());
            panel1.add(label1);
            panel1.add(comboBox1);
            for (int i = 0; i < champions.size(); i++) {
                 comboBox1.addItem(champions.get(i));           
            }
            comboBox1.setSelectedIndex(0);
            JLabel label2 = new JLabel("Role: ");
            final JComboBox comboBox2 = new JComboBox();
            panel1.add(label2);
            panel1.add(comboBox2);
            for (int i = 0; i < roles.size(); i++) {
                 comboBox2.addItem(roles.get(i));           
            }
            comboBox2.setSelectedIndex(0);
            JLabel label3 = new JLabel("Opponent: ");
            final JComboBox comboBox3 = new JComboBox();
            panel1.add(label3);
            panel1.add(comboBox3);
            for (int i = 0; i < champions.size(); i++) {
                 comboBox3.addItem(champions.get(i));           
            }
            comboBox3.setSelectedIndex(0);
            String[] dif = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
            JLabel label4 = new JLabel("Dificulty: ");
            final JComboBox comboBox4 = new JComboBox(dif);
            JPanel panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.add(label4);
            panel2.add(comboBox4);
            String[] result = new String[]{"Win", "Lose"};
            JLabel label5 = new JLabel("Result: ");
            final JComboBox comboBox5 = new JComboBox(result);
            panel2.add(label5);
            panel2.add(comboBox5);
            JLabel label6 = new JLabel("Score: ");
            JLabel slash1 = new JLabel("/");
            JLabel slash2 = new JLabel("/");
            final JTextField text1 = new JTextField(2);
            final JTextField text2 = new JTextField(2);
            final JTextField text3 = new JTextField(2);
            panel2.add(label6);
            panel2.add(text1);
            panel2.add(slash1);
            panel2.add(text2);
            panel2.add(slash2);
            panel2.add(text3);
            String[] gameType = new String[]{"normal", "ranked"};
            JLabel label7 = new JLabel("Game Type: ");
            final JComboBox comboBox7 = new JComboBox(gameType);
            JPanel panel3 = new JPanel();
            panel3.setLayout(new FlowLayout());
            panel3.add(label7);
            panel3.add(comboBox7);
            comboBox7.setSelectedIndex(1);
            final JLabel label8 = new JLabel("Current LP: ");
            final JTextField text8 = new JTextField(2);
            panel3.add(label8);
            panel3.add(text8);
            final JLabel label9 = new JLabel("Current league: ");
            final JComboBox comboBox9 = new JComboBox();
            panel3.add(label9);
            panel3.add(comboBox9);
            comboBox9.addItem(" ");
            for (int i = 0; i < leagues.size(); i++) {
                 comboBox9.addItem(leagues.get(i));           
            }
            comboBox9.setSelectedIndex(0);
            JLabel label0 = new JLabel("Notes: ");
            final JTextArea textArea = new JTextArea(5, 15);
            Font font = new Font("Verdana", Font.PLAIN, 11);
            textArea.setFont(font);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setBounds(10, 60, 780, 500);
            scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setPreferredSize(new Dimension(700, 150));
            JPanel panel4 = new JPanel();
            panel4.setLayout(new FlowLayout());
            panel4.add(label0);
            panel4.add(scrollPane);
            panel4.setBorder(new EmptyBorder(10, 250, 10, 300));
            JButton button = new JButton("Save");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!(text1.getText()).equals("") || !(text2.getText()).equals("") || !(text3.getText()).equals("") || !(text8.getText()).equals("")) {
                        String line = comboBox1.getSelectedItem() + "#" + comboBox2.getSelectedItem() + "#" + comboBox3.getSelectedItem() + "#" + comboBox4.getSelectedItem() + "#" + comboBox5.getSelectedItem() + "#" + text1.getText() + "/" + text2.getText() + "/" + text3.getText() + "#" + comboBox7.getSelectedItem() + "#" + text8.getText() + "#" + comboBox9.getSelectedItem() + "#" + textArea.getText() + "#";
                        output(line);
                        comboBox1.setSelectedIndex(0);
                        comboBox2.setSelectedIndex(0);
                        comboBox3.setSelectedIndex(0);
                        comboBox4.setSelectedIndex(0);
                        comboBox5.setSelectedIndex(0);
                        comboBox7.setSelectedIndex(1);
                        text1.setText("");
                        text2.setText("");
                        text3.setText("");
                        textArea.setText("");
                    } else {
                        JOptionPane.showMessageDialog(null, "One of text fields is not filled!");
                    }
                }
            });
            comboBox7.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if ((comboBox7.getSelectedItem()).equals("normal")) {
                        label8.setVisible(false);
                        text8.setVisible(false);
                        text8.setText(" ");
                        label9.setVisible(false);
                        comboBox9.setSelectedItem(" ");
                        comboBox9.setVisible(false);
                    } else {
                        label8.setVisible(true);
                        text8.setVisible(true);
                        label9.setVisible(true);
                        comboBox9.setVisible(true);
                        comboBox9.setSelectedIndex(1);
                    }
                }
            });
            frame.setLayout(new FlowLayout());
            JPanel panel = new JPanel();
            panel.add(panel1);
            panel.add(panel2);
            panel.add(panel3);
            panel.add(panel4);
            panel.add(button);
            panel.setBorder(new EmptyBorder(10, 10, 10, 10));
            panel.setPreferredSize(new Dimension(1000, 550));
            panel.setMaximumSize(new Dimension(1000, 550));
            JButton backButton = new backButton().create(frame, panel);
            panel.add(backButton);
            frame.add(panel);
            frame.pack();
            JMenuBar menuBar = new Menu(frame, panel, champions, roles, leagues);
            frame.setJMenuBar(menuBar);
            frame.setVisible(true);
            Rectangle rec = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
            int width = (int) rec.getWidth();
            int height = (int) rec.getHeight();
            frame.setBounds(1, 3, width, height);
            frame.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentMoved(ComponentEvent e) {
                    frame.setLocation(1, 3);
                }
            });
            
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
